package Comp
{
	import mx.controls.Alert;
	
	
	public  class automata
	{
		// 한글  캔디
		public static var initial:String = "rRseEfaqQtTdwWczxvg";		       		
		public static var medial:Array = new Array( 'k', 'o', 'i', 'O', 'j', 'p', 'u', 'P', 'h', 'hk', 'ho', 'hl', 'y', 'n', 'nj', 'np', 'nl', 'b', 'm', 'ml', 'l' );
		public static var final:Array = new Array( 'r', 'R', 'rt', 's', 'sw', 'sg', 'e', 'f', 'fr', 'fa', 'fq', 'ft', 'fx', 'fv', 'fg', 'a', 'q', 'qt', 't', 'T', 'd', 'w', 'c', 'z', 'x', 'v', 'g' );
		
		public static var bEngTbl:Array = new Array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0');
		
		// 영어 +숫자 캔디
		public static var engInitial:String = "rRseEfaqQtTdwWczxvg";
				        		
		//public static var SNAPS_TEST:Boolean = false;
		public function automata()
		{
		}
		
		public static function makeCandy(isEngCode:String):String {
			if(isEngCode == '1') {
				var minLength:int = 15;
				var d:int;
				var e:int;
				var sCandy:String="";
				// 생성 글자 길이
				var candyLength:int = Math.round(Math.random() *10)+minLength;
				for(var i:uint=0; i < candyLength; i++) {
					d = Math.round(Math.random() *1000);
					e = d%62;		
					sCandy= sCandy+bEngTbl[e];						
				}				
			} else {
				// 변수 초기화
				var initialCode:int = 0;
				var medialCode:int  = 0;
				var finalCode:int   = 0;		
				
				for(var i:uint=0; i < candyLength; i++) {
					initialCode = getCode2( 'initial', Math.round(Math.random() *18));
					medialCode = getCode2( 'medial', Math.round(Math.random() *21));
					finalCode = getCode2( 'final', Math.round(Math.random() *27));
					trace("initialCode:"+initialCode+", medialCode:"+medialCode+", finalCode:"+finalCode);
					sCandy = sCandy+String.fromCharCode( 0xAC00 + initialCode + medialCode + finalCode );
				}
				//taCandy.text = sCandy;
				//alert("sCandy@ : " + sCandy + " , initialCode:"+initialCode+", medialCode : " + medialCode + ", finalCode:"+finalCode);
			}
			return sCandy;
		}
		
		public static function getCode2( type:String, indexChar:uint ):int
		{
		
		        var returnCode:int; // 리턴 코드 저장 변수
		        var isFind:Boolean = false; // 문자를 찾았는지 체크 변수
		
		        if( type == 'initial' )
		        {				
		                returnCode = initial.indexOf(initial.substr( indexChar, 1 )) * 21 * 28;
		//				alert("returnCode : " + returnCode + ", indexChar : " + indexChar +",  initial.substr( indexChar, 1 ) : " + initial.substr( indexChar, 1 ));
		                isFind = true;
		        }
		        else if( type == 'medial' )
		        {
		                for( var i:uint = 0; i < medial.length; i++ )
		                {
		                        if( medial[i] == medial[indexChar] )
		                        {
		//								alert("indexChar : " + indexChar +", medial[indexChar] : " +medial[indexChar]  );
		                                returnCode = i * 28;
		                                isFind = true;
		                                break;
		                        }
		                }
		        }
		        else if( type == 'final' )
		        {
		                for( var ii:uint = 0; i < final.length; ii++ )
		                {
		                        if( final[ii] == final[indexChar] )
		                        {
		//								alert("indexChar : " + indexChar +", final[indexChar] : " +final[indexChar]  );
		                                returnCode = ii * 21;
		                                isFind = true;
		                                break;
		                        }
		                }
		        }
		        else
		        {
		                Alert.show("잘못된 타입입니다.");
		        }
		
		        if( isFind == false ) returnCode = -1; // 값을 찾지 못했을 경우 -1 리턴
		        return returnCode;
		}		
	}
}